package dao;

import hibernateUtils.HibernateUtil;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import beans.PlanProduccionBean;

public class HibernatePlanProduccionDAO {
	private static HibernatePlanProduccionDAO instancia;
	private static SessionFactory sf;

	private HibernatePlanProduccionDAO() {

	}

	public static HibernatePlanProduccionDAO getInstancia() {
		if (instancia == null) {
			sf = HibernateUtil.getSessionFactory();
			instancia = new HibernatePlanProduccionDAO();
		}
		return instancia;
	}

	public void grabarPlanProduccion(PlanProduccionBean fb) {
		Session session = sf.openSession();
		session.beginTransaction();
		session.save(fb);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public PlanProduccionBean buscarPlanProduccion(PlanProduccionBean fb) {
		Session session = sf.openSession();
		PlanProduccionBean f = (PlanProduccionBean) session.get(
				PlanProduccionBean.class, fb.getId());
		session.close();
		return f;
	}

	public void update(PlanProduccionBean ppb) {
		Session sesion = sf.openSession();
		sesion.beginTransaction();
		sesion.update(ppb);
		sesion.flush();
		sesion.getTransaction().commit();
		sesion.close();

	}

	public List<PlanProduccionBean> list() {
		Session sesion = sf.openSession();
		sesion.beginTransaction();
		Query query = sesion.createQuery("from PlanProduccionBean");
		List<PlanProduccionBean> planes = new ArrayList<PlanProduccionBean>();
		planes = query.list();
		sesion.getTransaction().commit();
		return planes;
	}

	public void merge(PlanProduccionBean ppb) {
		Session sesion = sf.openSession();
		sesion.beginTransaction();
		sesion.merge(ppb);
		sesion.flush();
		sesion.getTransaction().commit();;
		sesion.close();
		
	}
}
